from django.db import models

# Create your models here.
from lib.orm import ModelMixin


class User(models.Model):
  phonenum = models.CharField(max_length=16, unique=True)
  key=models.CharField(max_length=16, default='', verbose_name='密码盐')
  jykey=models.CharField(max_length=16, default='', verbose_name='交易密码盐')
  passwd = models.CharField(max_length=64)
  jypasswd = models.CharField(max_length=32, verbose_name='交易密码', default='')
  recommend=models.CharField(max_length=32,verbose_name='上级推荐人phone')
  user_price = models.FloatField(default=20.0)    #角色默认的代币
  user_profit = models.IntegerField(default=0)   #角色获得的总收益

  state=models.BooleanField(default=True)
  createtime=models.DateTimeField(auto_now_add=True)

  nickname = models.CharField(max_length=16, verbose_name='昵称', default='hello')
  avatar = models.CharField(max_length=256, verbose_name='用户头像地址', default='http://124.70.219.207:2021/123.jpg')
  ismanager=models.BooleanField(default=False)

  has_wx=models.BooleanField(default=False,verbose_name='是否有WX')
  has_ali=models.BooleanField(default=False,verbose_name='是否有ZFB')
  has_yhk=models.BooleanField(default=False,verbose_name='是否有HYK')
  has_renzheng = models.BooleanField(default=False, verbose_name='是否实名认证')

  def to_dict(self):
    return {
      'id':self.id,
      'phonenum':self.phonenum,
      'key':self.key,
      'jykey':self.jykey,
      'avatar':self.avatar,
      'nickname':self.nickname,
      "state":self.state,
      'recommend':self.recommend,
      'has_renzheng':self.has_renzheng,
      'createtime':self.createtime.strftime("%Y-%m-%d %H:%M:%S")
    }

  @property
  def renzheng(self):
    if not hasattr(self, '_renzheng'):
      self._renzheng, _ = Renzheng.objects.get_or_create(id=self.id)
    return self._renzheng

  @property
  def ali(self):
    if not hasattr(self, '_ali'):
      self._ali, _ = Ali.objects.get_or_create(id=self.id)
    return self._ali

  @property
  def yinghang(self):
    if not hasattr(self, '_yinghang'):
      self._yinghang, _ = Yinhang.objects.get_or_create(id=self.id)
    return self._yinghang

  @property
  def wx(self):
    if not hasattr(self, '_wx'):
      self._wx, _ = Wx.objects.get_or_create(id=self.id)
    return self._wx

class Renzheng(models.Model,ModelMixin):
  idname = models.CharField(max_length=32, verbose_name='姓名', default='')
  idcard = models.CharField(max_length=32, verbose_name='身份证号', default='')


class Ali(models.Model,ModelMixin):
  aliname=models.CharField(max_length=32, verbose_name='支付宝名',default='')
  alinum=models.CharField(max_length=16, verbose_name='支付宝账号',default='')
  aliskm=models.CharField(max_length=256,verbose_name='支付宝收款码',default='')

class Wx(models.Model,ModelMixin):
  wxname=models.CharField(max_length=32, verbose_name='微信名',default='')
  wxnum=models.CharField(max_length=32, verbose_name='微信账号',default='')
  wxskm=models.CharField(max_length=256,verbose_name='微信收款码',default='')

class Yinhang(models.Model,ModelMixin):
  yhname=models.CharField(max_length=32, verbose_name='姓名',default='')
  yinghang=models.CharField(max_length=32, verbose_name='开户行',default='')
  yhnum=models.CharField(max_length=32,verbose_name='银行卡号',default='')

class Roles(models.Model,ModelMixin):
  role_name=models.CharField(max_length=16)
  role_desc=models.CharField(max_length=16)
  # 用户表和角色表是一对多关系
  user_id=models.IntegerField(default='')

  # def role_perm(self):
  #   """当前role具有的权限"""
  #   relations= Permission.objects.filter(roles_id=self.id)
  #   perm_id_list=[r.roles_id for r in relations]
  #   return Permission.objects.filter(id__in=perm_id_list)

  def role_perm(self):
    """当前role具有的所有权限"""
    relations= RolePermRelation.objects.filter(roles_id=self.id)
    perm_id_list=[r.perm_id for r in relations]
    return Permission.objects.filter(id__in=perm_id_list)

  def has_perm(self,auth_name):
    """检查是否具有某种权限"""
    perm=Permission.objects.get(auth_name=auth_name)
    return RolePermRelation.objects.filter(role_id=self.id,perm_id=perm.id).exists()

  @property
  def role(self):
    """一对多关系映射 用户-角色"""
    if not hasattr(self, '_role'):
      self._role = User.objects.get(id=self.user_id)
    return self._role

class Permission(models.Model,ModelMixin):
  auth_name=models.CharField(max_length=16)
  path=models.CharField(max_length=32)
  level = models.IntegerField()
  pid = models.IntegerField()

  # #角色表和权限表是一对多关系
  # roles_id=models.IntegerField(default='')

  # @property
  # def perm(self):
  #   """一对多关系映射 角色-权限"""
  #   if not hasattr(self, '_perm'):
  #     self._perm = Roles.objects.get(id=self.roles_id)
  #   return self._perm

  @property
  def children(self):
    child_perm=Permission.objects.filter(pid=self.id)
    return child_perm

class RolePermRelation(models.Model):
  """角色-权限 多对多关系表"""
  role_id=models.IntegerField()
  perm_id=models.IntegerField()






